package com.gator.desig_mode.mvc;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc
 */
public class DispatchServlet {


  List<HandlerAdapter> handlerAdapters;

  public DispatchServlet() {
    handlerAdapters = new ArrayList<>();
    handlerAdapters.add(new AnnotationHandlerAdapter());
    handlerAdapters.add(new HttpHandlerAdapter());
    handlerAdapters.add(new SimpleAdapter());
  }

  public void doDispatch() {

    AnnotationController controller = new AnnotationController();
    HandlerAdapter handler = getHandler(controller);
    handler.handle(controller);

  }

  private HandlerAdapter getHandler(Controller controller) {
    for (HandlerAdapter adapter :
        handlerAdapters) {
      if (adapter.supports(controller)) {
        return adapter;
      }
    }

    return null;
  }

}
